//
//  NetworkManager.m
//  BaseLib
//
//  Created by CPX on 2018/5/21.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "NetworkManager.h"
#import "AFNetworking.h"

#define NetworkTimeOutIntervel 20

NSErrorDomain const EgddErrorDomin = @"app.response.failure";
NSInteger const AppResponseFailureCode = 1;
NSString *const AppResponseFailureText = @"Reqest Failure";

/**
 网络库封装
 @author yzl
 */
@implementation NetworkManager

+ (instancetype)sharedManager{
    static NetworkManager *mgr;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mgr = [NetworkManager new];
    });
    return mgr;
}

+ (void)GETWithURL:(NSString *)url params:(NSDictionary *)params success:(Success)success failure:(Failure)failure{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer.timeoutInterval = NetworkTimeOutIntervel;
    
    [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
}

+ (void)GETWithURL:(NSString *)url header:(NSDictionary *)headers params:(NSDictionary *)params success:(Success)success failure:(Failure)failure{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer.timeoutInterval = NetworkTimeOutIntervel;

    for (NSString *key in headers.allKeys) {
        [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];
    }
    
    [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
}

+ (void)GETWithURL:(NSString *)url params:(NSDictionary *)params progress:(DownloadProgress)progress success:(Success)success failure:(Failure)failure{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer.timeoutInterval = 20;
        
    [manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"ok");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void)GETWithURL:(NSString *)url header:(NSDictionary*)headers params:(NSDictionary *)params  progress:(DownloadProgress)progress success:(Success)success failure:(Failure)failure{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer.timeoutInterval = 20;
    for (NSString *key in headers.allKeys) {
        [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];
    }
    [manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"ok");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void)POSTWithURL:(NSString *)url params:(id)params success:(Success)success failure:(Failure)failure{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = NetworkTimeOutIntervel;
    
    [manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
}

+ (void)DownloadFile:(NSString *)url progress:(DownloadProgress)progress destination:(Destination)destination completionHandler:(CompletionHandler)completion name:(NSString*)name{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    NSURLSessionDownloadTask *downloadTask =  [manager downloadTaskWithRequest:request progress:progress destination:destination completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        
        completion(response,filePath,error);
        
    }];
    
    [downloadTask resume];
}

- (void)startMonitoring{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

- (void)stopMonitoring{
    [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
}

- (NSMutableDictionary*)keyForTask{
    if (!_keyForTask) {
        _keyForTask = [NSMutableDictionary dictionary];
    }
    return _keyForTask;
}

@end
